package com.example.yrdev.ordering_system.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Model.Good;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;

/**
 * Created by luke on 11/18/16.
 */

public class ShoppingCartAdapter extends BaseAdapter {
LayoutInflater inflater;
ArrayList<Good> shoppingList;
    ArrayList<Integer> purchaseCountList;

   private ShoppingCartListener shoppingCartListener;

   class ViewHolder{
        Button addButton;
        Button minButton;
        TextView goodName;
        TextView totalPrice;
       TextView priceOut;
       TextView currentPurchaseCountTxt;
        TextView storedNumber;
        Good respGood;
       Button delButton;

   }
   public  interface ShoppingCartListener{
        void reloadPrice();
        void goBack();
        void calcVal(String viewPosition, int butId);
        void delItem(String viewPosition);



    }

    public void setShoppingCartListener(ShoppingCartListener shoppingCartListener) {
        this.shoppingCartListener = shoppingCartListener;
    }

    public ArrayList<Good> getShoppingList() {
        return shoppingList;
    }

    public ShoppingCartAdapter(Context context, ArrayList<Good > shoppingList){

     inflater= LayoutInflater.from(context);
     this.shoppingList=shoppingList;
        purchaseCountList=new ArrayList<>();


    }
    public void removeItem(int position){
        Good respGood=shoppingList.get(position);

            GlobalFunction.printTextByTag("remove index:"+position+"");
        this.    shoppingList.remove(position);

        notifyDataSetChanged();

    }
    @Override
    public int getCount() {
        return shoppingList.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Good getItem(int position) {
        return shoppingList.get(position);

    }
   public void delGood(int position){
       shoppingList.remove(position);
       notifyDataSetChanged();

       if (shoppingList.size()==0){

           if (this.shoppingCartListener!=null)
               this.shoppingCartListener.goBack();

       }
   }
   private  Good good;
   private Integer currentPurchaseCount;
  private TextView currentPurchaseCountTxt;
 private TextView total_price;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;


        if (convertView==null){

            convertView=inflater.inflate(R.layout.shopping_cart_row,null);
            holder=new ViewHolder();

            holder.addButton=(Button)convertView.findViewById(R.id.min);


            holder.minButton=(Button)convertView.findViewById(R.id.add);


            holder.delButton=(Button)convertView.findViewById(R.id.delGood);

            holder.goodName=(TextView)convertView.findViewById(R.id.goodName);
            holder.totalPrice=(TextView)convertView.findViewById(R.id.total_price);
            holder.storedNumber=(TextView)convertView.findViewById(R.id.storedNumber);
            holder.priceOut=(TextView)convertView.findViewById(R.id.priceOut);
            holder.currentPurchaseCountTxt=(TextView)convertView.findViewById(R.id.purchaseCount);


            convertView.setTag(holder);


        }else {
        holder=(ViewHolder)convertView.getTag();

        }

        holder.respGood=getItem(position);
        good=getItem(position);

        TextView tempStoreNum=(TextView)convertView.findViewById(R.id.tempStoredNumber);
        tempStoreNum.setText("x"+good.getPurchaseCount());

        TextView tempTotPrice=(TextView)convertView.findViewById(R.id.tempTotalPrice);
        tempTotPrice.setText(good.getTotalPrice()+"元");




        holder.addButton.setTag(position+"");
        holder.minButton.setTag(position+"");
        holder.delButton.setId(position);
        currentPurchaseCount=good.getPurchaseCount();
        purchaseCountList.add(currentPurchaseCount);

        holder.goodName.setText(good.getName());
        holder.priceOut.setText(good.getPriceOut()+"元");
        holder.storedNumber.setText("x"+good.getStoredNumber());


        currentPurchaseCountTxt=holder.currentPurchaseCountTxt;
        currentPurchaseCountTxt.setText(String.valueOf(currentPurchaseCount));


        currentPurchaseCount=good.getPurchaseCount();

        total_price=holder.totalPrice;

        total_price.setText(good.getTotalPrice()+"元");


       holder.minButton.setOnClickListener(calcPurchaseCount());
       holder.addButton.setOnClickListener(calcPurchaseCount());

        holder.delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (shoppingCartListener!=null)
                    shoppingCartListener.delItem(v.getId()+"");
            }
        });

        return convertView;
    }
   private View.OnClickListener calcPurchaseCount(){

       return new View.OnClickListener() {
           @Override
           public void onClick(View v) {


               if (shoppingCartListener!=null){

                   shoppingCartListener.calcVal((String)v.getTag(),v.getId());

               }

             //  reloadCorrespondView();
           }
       };

   }


}
